package com.example.invoice_contrast.controller;

import com.example.invoice_contrast.bean.DateLakeElectronicInvoice;
import com.example.invoice_contrast.bean.ElectronicInvoice;
import com.example.invoice_contrast.bean.Invoice;
import com.example.invoice_contrast.service.DateLakeElectronicInvoiceService;
import com.example.invoice_contrast.service.ElectronicInvoiceService;
import com.example.invoice_contrast.service.ProblemInvoiceService;
import com.example.invoice_contrast.utils.DateUtils;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;


@Component
@Order(1)
public class TestDataComparisonController implements ApplicationRunner {

    @Autowired
    private ElectronicInvoiceService electronicInvoiceService;

    @Autowired
    private DateLakeElectronicInvoiceService dateLakeElectronicInvoiceService;

    @Autowired
    private ProblemInvoiceService problemInvoiceService;

    private static final Logger logger = Logger.getLogger(TestDataComparisonController.class.getName());

    /**
     * 查询电子发票表的数据
     * @param date
     * @throws IOException
     */
    public Set<ElectronicInvoice> testSelectElectronicInvoice(String date) throws IOException{
        Set<ElectronicInvoice> byEI = electronicInvoiceService.findByEI(date);
            return byEI;
    }

    /**
     * 查询数据胡电子发票表的数据
     * @param date
     * @throws IOException
     */
    public Set<DateLakeElectronicInvoice> testSelectDateLakeElectronicInvoice(String date) throws IOException{
        Set<DateLakeElectronicInvoice> byDLEI = dateLakeElectronicInvoiceService.findByDLEI(date);
        return byDLEI;
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        String s = DateUtils.dateUtily();
//        String s = "2025-02-20";
        logger.info("参数时间获取前一天日期yyyy-MM-dd : " + s );
        Set<ElectronicInvoice> electronicInvoices = testSelectElectronicInvoice(s);// 查询电子发票表的数据
        Set<DateLakeElectronicInvoice> dateLakeElectronicInvoices = testSelectDateLakeElectronicInvoice(s);
        logger.info(s + " ods_omd_electronicinvoice_df(MQ)的数据条数 " + electronicInvoices.size());
        logger.info(s + " ods_omd_tt_electron_invoice_df(数据湖)的数据条数 " + dateLakeElectronicInvoices.size());
        if (electronicInvoices.size() != dateLakeElectronicInvoices.size()){ // 两张表的数据量不相等

            Set<Invoice> invoicesSet1 = new HashSet<>(); // 对应MQ
            Set<Invoice> invoicesSet2 = new HashSet<>(); // 对应数据湖
            // 先将dateLakeElectronicInvoices和electronicInvoices中存储的createDate字段转换成 'yyyy-MM-dd'的格式
            for (ElectronicInvoice electronicInvoice : electronicInvoices) {
                invoicesSet1.add(new Invoice(
                        electronicInvoice.getInvoiceCode(),
                        electronicInvoice.getInvoiceNo(),
                        electronicInvoice.getCreateDate()
                ));
            }
            for (DateLakeElectronicInvoice dateLakeElectronicInvoice : dateLakeElectronicInvoices) {
                invoicesSet2.add(new Invoice(
                        dateLakeElectronicInvoice.getInvoiceCode() ,
                        dateLakeElectronicInvoice.getInvoiceNo(),
                        dateLakeElectronicInvoice.getCreateDate()
                ));
            }
            if (dateLakeElectronicInvoices.size() > electronicInvoices.size()){ // 数据湖接的表的数据多
                // 找出数据湖中有但MQ中没有的数据
                invoicesSet2.removeAll(invoicesSet1);
                // TODO 将存在疑问的数据插入到数据库中
                logger.info( " MQ比数据湖少的数据 :" );
                invoicesSet2.forEach(System.out::println);
                for (Invoice invoice : invoicesSet2) {
                    problemInvoiceService.deleteInvoice(s);
                }
                for (Invoice invoice : invoicesSet2) {
                    problemInvoiceService.insertInvoice(invoice);
                }
            } else { // MQ接的表数据多
                invoicesSet1.removeAll(invoicesSet2);
                logger.info(" 数据湖比MQ少的数据 :");
                invoicesSet1.forEach(System.out::println);
                for (Invoice invoice : invoicesSet1) {
                    problemInvoiceService.deleteInvoice(s);
                }
                for (Invoice invoice : invoicesSet1) {
                    problemInvoiceService.insertInvoice(invoice);
                }
            }
            System.exit(0);
        } else { // 两张表的数据量相等，正常现象。
            logger.info("情况正常,程序结束");
            System.exit(0);
        }
//        testSelectDateLakeElectronicInvoice(s); // 查询数据胡电子发票表的数据
    }
}











